package com.example.AdminComponent;

import com.example.Utils.HttpUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class PassJFrame extends JFrame {
    public PassJFrame(String user,String passWd){
        setVisible(true);
        super.setTitle("修改密码");
        setSize(300,200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        BoxUlit PasswdBox=new BoxUlit("旧密码");
        BoxUlit newPassBox=new BoxUlit("新密码");
        Box Big=Box.createVerticalBox();
        Big.add(Box.createVerticalStrut(25));
        Big.add(PasswdBox);
        Big.add(Box.createVerticalStrut(10));
        Big.add(newPassBox);
        JButton Ok=new JButton("确定");
        JButton No=new JButton("取消");
        Box buttonBox = Box.createHorizontalBox();
        buttonBox.add(Ok);
        buttonBox.add(Box.createHorizontalStrut(20));
        buttonBox.add(No);
        Big.add(Box.createVerticalStrut(30));
        Big.add(buttonBox);
        JPanel jPanel=new JPanel();
        jPanel.add(Big);
        add(jPanel);
        Ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int i=0;
                //判断密码是否相等
                if(passWd.equals(PasswdBox.idText.getText().trim())&&i<3) {
                    Map<String,String> map=new HashMap<>();
                    map.put("id",user);
                    map.put("passwd",newPassBox.idText.getText());
                    HttpUtil.sendPost("http://localhost:8080/UpdatePassWord",map);
                    JOptionPane.showMessageDialog(null, "修改成功!请重新启动");
                    System.exit(0);
                }else {
                    i++;
                    JOptionPane.showMessageDialog(null, "密码错误，请重新输入");
                }
            }
        });
        No.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

    }
}
